/**
 * @author: akolonitsky
 * @date: 12/5/11 11:19 AM
 */
package org.kolonitsky.alexey.mvcext.view
{
    import com.asual.swfaddress.SWFAddress;
    import com.asual.swfaddress.SWFAddressEvent;

    import controller.UserPasswordCommand;

    import flash.external.ExternalInterface;

    import model.UserProxy;
    import model.vo.UserVO;

    import org.kolonitsky.alexey.debug.DebugWrapper;

    import org.kolonitsky.alexey.mvcext.NotificationMap;
    import org.kolonitsky.alexey.mvcext.events.PageDescriptor;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;
    import org.puremvc.as3.patterns.observer.Notification;

    /**
 *  SWF Address mediator catch SWITCH_PAGE notifications and change
 *  browser URL. It also pars browser URL on start and open requaired page
 *  by sending SWITCH_PAGE notification.
 */
public class ContainerMediator extends Mediator
{
    public static const NAME:String = "containerMediator";

    public function ContainerMediator()
    {
        super(NAME, null);
        DebugWrapper.trace("INFO: ContainerMediator. Created");
    }

    public var currentPath:String = "";




    //---------------------------------------------------------------------
    //
    //  Override mediator methods
    //
    //---------------------------------------------------------------------

    override public function onRegister():void
    {
        DebugWrapper.trace("INFO: ContainerMediator. Registred");
        
        if (ExternalInterface.available)
        {
            ExternalInterface.addCallback("signout", js_signout);
        }
        
        SWFAddress.addEventListener(SWFAddressEvent.CHANGE, address_changeHandler);
    }

    override public function onRemove():void
    {
        DebugWrapper.trace("INFO: ContainerMediator. Removed");
        SWFAddress.removeEventListener(SWFAddressEvent.CHANGE, address_changeHandler);
    }

    override public function listNotificationInterests():Array
    {
        return [
            NotificationMap.SWITCH_PAGE,
            NotificationMap.UPDATE_PAGE,
            UserProxy.USER_AUTHORIZATION,
            UserProxy.USER_SIGNOUT
        ]
    }

    override public function handleNotification(notification:INotification):void
    {
        switch (notification.getName())
        {
            case NotificationMap.SWITCH_PAGE:
            {
                var page:PageDescriptor = notification.getBody() as PageDescriptor;
                currentPath = page.pageId;
                SWFAddress.setValue("/" + page.pageId);
                break;
            }

            case NotificationMap.UPDATE_PAGE:
            {
                sendUpdateNotification();
                break;
            }

            case UserProxy.USER_SIGNOUT:
            {
                callJavaScript("embedApplication");
                break;
            }

            case UserProxy.USER_AUTHORIZATION:
            {
                var user:UserVO = notification.getBody() as UserVO;
                if (user && user.authorized)
                {
                    var userName:String = user.login || user.firstName || user.email;
                    callJavaScript("setUserName('" + userName + "')");
                }
                break;
            }
        }
    }





    //---------------------------------------------------------------------
    //
    //  Private logic
    //
    //---------------------------------------------------------------------

    private function address_changeHandler(event:SWFAddressEvent=null):void
    {
        var screenID:String = Config.defaultPageId;

        if (event.pathNames && event.pathNames.length)
        {
            screenID = event.pathNames.pop();
            if (screenID == currentPath)
                return;
        }

        DebugWrapper.trace("INFO: ContainerMediator.address_changeHandler: " + screenID);
        sendNotification(NotificationMap.SWITCH_PAGE, new PageDescriptor(screenID));
    }

    private function sendUpdateNotification():void
    {
        if (currentPath)
        {
            var page:PageDescriptor = new PageDescriptor(currentPath);
            sendNotification(NotificationMap.SWITCH_PAGE, page);
        }
    }

        private function callJavaScript(functionName:String):void
        {
            if (ExternalInterface.available)
            {
                ExternalInterface.call(functionName);
            }
            else
            {
                DebugWrapper.trace("ERROR:")
            }
        }
        
        private function js_signout():void
        {
            sendNotification(Config.USER_SIGN_OUT);
        }
}
}
